package com.jsb.laundry.common.mq.confirm_receive_deadLetter;

import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.enums.OrderStatus;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.common.utils.http.HttpUtil;
import com.jsb.laundry.dao.OrderDao;
import com.jsb.laundry.model.entity.JsbOrders;
import com.jsb.laundry.model.vo.PrePayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户确认收货( 死信队列消费者 )
 */

@Slf4j
@Component
public class ConfirmReceiveOrderConsumer {

    @Resource
    private OrderDao orderDao;

    /**
     * 用户在7天内 点击确认收货后
     */
//    @RabbitListener(queues = UserConfirmReceiveOrderConfig.userConfirmReceiveOrderQueue)
//    @RabbitListener(
//            bindings = @QueueBinding(
//                    value = @Queue(name = UserConfirmReceiveOrderConfig.userConfirmReceiveOrderQueue),
//                    exchange = @Exchange(name = ExchangeTypes.DIRECT),
//                    key = {UserConfirmReceiveOrderConfig.userConfirmReceiveOrderRoutingKey}
//            ))
//    @RabbitListener(
//            bindings = @QueueBinding(
//                    value = @Queue(name = "topic.queue2"),
//                    exchange = @Exchange(name = "topic", type = ExchangeTypes.TOPIC),
//                    key = "#.news"
//            ))
//    public void confirmReceiveOrderMessage1(Message message, PrePayVo prePayVo) {
//        log.info("用户在7天内主动点击确认收货后,接受到队列{}的消息:{}", message.getMessageProperties().getConsumerQueue(), prePayVo);
//        System.out.println(prePayVo);
//    }

    /**
     * 7天后系统自动确认收货
     *
     * @param message
     * @param prePayVo
     */
    @RabbitListener(queues = UserConfirmReceiveOrderConfig.userConfirmReceiveOrderDLQueue)
    public void confirmReceiveOrderMessage2(Message message, PrePayVo prePayVo) {
        log.info("7天后系统自动确认收货,接受到队列{}的消息:{}", message.getMessageProperties().getConsumerQueue(), prePayVo);

        String orderId = prePayVo.getOrderId();

        JsbOrders orderInfo = orderDao.getOrderInfo(orderId);
        /**
         *  1.订单未被接收促发系统分账
         *  2.添加jsb_order_status_log记录
         *  3.填充jsb_orders时间success_at
         */
        if (orderInfo!=null && JSBUtil.isEmptyObject(orderInfo.getSuccessAt())) {
            int var0 = orderDao.updParentOrderSuccessAt(orderId);
            int var1 = orderDao.addOrderStatusLog(orderId, OrderStatus.ORDER_SUCCESS.getCode(), "7天后系统自动确认收货", "2");
            /**
             * TODO 系统分账
             */
            Map<String,String> map = new HashMap();
            map.put("orderId",orderId);
            String res = HttpUtil.sendPost("http://127.0.0.1:20000/riderOrder/qs/splitSettled", null, JSONObject.toJSONString(map));

            log.info("更新jsb_orders时间success_at:【入参:{},出参:{}】",orderId,var0);
            log.info("添加jsb_order_status_log记录:【入参:{},{},{},{},出参:{}】",orderId, OrderStatus.ORDER_SUCCESS.getCode(), "7天后系统自动确认收货", "2",var1);
            log.info("促发系统分账:【入参:{},出参:{}】",orderId,res);
        }
    }

}
